import 'package:dmz/base/pageWidget/base_stateful_widget.dart';
import 'package:dmz/base/widget/defult_widget.dart';
import 'package:dmz/business/mine/controller/news_center_controller.dart';
import 'package:dmz/business/mine/model/news_model.dart';
import 'package:dmz/business/mine/widget/garage_widgets.dart';
import 'package:dmz/business/mine/widget/news_center_widgets.dart';
import 'package:dmz/utils/sp_utils.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';

class NewsCenteListWidget extends StatefulWidget {
  final int tag;
  const NewsCenteListWidget({super.key, required this.tag});

  @override
  State<NewsCenteListWidget> createState() => _NewsCenteListWidgetState();
}

class _NewsCenteListWidgetState extends State<NewsCenteListWidget> {
  final ScrollController _scrollController = ScrollController();
  bool _isLoadingMore = false;
  int index = 1;

  Future<void> _loadMore() async {
    var controller = Get.find<NewsCenterController>();
    var datas = controller.alldatas[widget.tag];

    if (_isLoadingMore) return;
    if (datas.length % 20 > 0) return;

    _isLoadingMore = true;
    index++;
    await controller.getAcceptShareList(widget.tag, index);
    _isLoadingMore = false;
  }

  @override
  void initState() {
    super.initState();
    _scrollController.addListener(_scrollListener);
  }

  void _scrollListener() {
    if (_scrollController.position.extentAfter < 500) {
      _loadMore();
    }
  }

  @override
  void dispose() {
    _scrollController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return GetBuilder<NewsCenterController>(
        id: 'news${widget.tag}',
        builder: (controller) {
          return Column(children: [
            NewsCenterWidgets.selectTimeWidget(controller.timesStr[widget.tag],
                widget.tag, (tag) => controller.selectDate(tag)),
            Expanded(
                child: RefreshIndicator(
                    color: const Color(0xFF999999),
                    onRefresh: () =>
                        controller.getAcceptShareList(widget.tag, 0),
                    child: ListView.builder(
                        controller: _scrollController,
                        physics: const AlwaysScrollableScrollPhysics(),
                        itemCount: controller.alldatas[widget.tag].isEmpty
                            ? 1
                            : controller.alldatas[widget.tag].length + 1,
                        itemBuilder: (content, index) {
                          if (controller.alldatas[widget.tag].isEmpty) {
                            return GarageWidgets.emptyWidget()
                                .marginOnly(top: 200.sp);
                          }

                          if (index == controller.alldatas[widget.tag].length) {
                            return _isLoadingMore
                                ? const Padding(
                                    padding: EdgeInsets.all(8.0),
                                    child: Center(
                                        child: CircularProgressIndicator(
                                      color: Color(0xFF999999),
                                    )),
                                  )
                                : const SizedBox.shrink();
                          }

                          return widget.tag == 2
                              ? otherWidget(
                                      controller.alldatas[widget.tag][index])
                                  .addGestureDetector(() =>
                                      controller.clickNews(
                                          controller.alldatas[widget.tag]
                                              [index],
                                          widget.tag))
                              : faultCell(
                                      controller.alldatas[widget.tag][index])
                                  .addGestureDetector(() =>
                                      controller.clickNews(
                                          controller.alldatas[widget.tag]
                                              [index],
                                          widget.tag));
                        })))
          ]);
        });
  }

  Widget faultCell(NewsModel data) {
    return Container(
            padding: EdgeInsets.only(left: 22.sp, right: 22.sp),
            color: const Color(0xFF434650),
            child: Column(crossAxisAlignment: CrossAxisAlignment.start, children: [
              Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  crossAxisAlignment: CrossAxisAlignment.center,
                  children: [
                    DefaultText('${'车架号'.tr}：${data.frame_code ?? ''}', 28.sp,
                        textColor: const Color(0xFF23C565)),
                    DefaultText(
                        SPUtils.timeIntToTimeStr(data.created_at ?? 0,
                            attern: 'yyyy-MM-dd HH:mm'),
                        28.sp,
                        textColor: const Color(0xFF999999))
                  ]).marginOnly(top: 32.sp, bottom: 6.sp),
              Row(children: [
                if (data.flag == 0)
                  Container(
                          width: 20.sp,
                          height: 20.sp,
                          color: const Color(0xFFFC720B))
                      .didClipRRectAll(20.sp)
                      .marginOnly(right: 18.sp),
                Expanded(
                    child: DefaultText(data.title ?? '', 48.sp,
                        textColor: const Color(0xFFF9F9F9),
                        fontWeight: FontWeight.w700)),
              ]),
              DefaultText(data.content ?? '', 28.sp,
                      textColor: const Color(0xFFF9F9F9),
                      fontWeight: FontWeight.w700)
                  .marginOnly(top: 6.sp, bottom: 32.sp),
            ]))
        .didClipRRectAll(56.sp)
        .marginOnly(left: 32.sp, right: 32.sp, bottom: 20.sp, top: 20.sp);
  }

  Widget otherWidget(NewsModel data) {
    return Container(
            padding: EdgeInsets.only(left: 22.sp, right: 22.sp),
            color: const Color(0xFF434650),
            child: Column(crossAxisAlignment: CrossAxisAlignment.start, children: [
              Row(children: [
                if (data.flag == 0)
                  Container(
                          width: 20.sp,
                          height: 20.sp,
                          color: const Color(0xFFFC720B))
                      .didClipRRectAll(20.sp)
                      .marginOnly(right: 18.sp),
                Expanded(
                    child: DefaultText(data.title ?? '', 48.sp,
                        textColor: const Color(0xFFF9F9F9),
                        fontWeight: FontWeight.w700)),
                const Expanded(child: SizedBox()),
                DefaultText(
                    SPUtils.timeIntToTimeStr(data.created_at ?? 0,
                        attern: 'yyyy-MM-dd HH:mm'),
                    28.sp,
                    textColor: const Color(0xFF999999))
              ]).marginOnly(top: 32.sp),
              DefaultText(data.content ?? '', 28.sp,
                      textColor: const Color(0xFFF9F9F9),
                      fontWeight: FontWeight.w700)
                  .marginOnly(top: 6.sp, bottom: 32.sp),
            ]))
        .didClipRRectAll(56.sp)
        .marginOnly(left: 32.sp, right: 32.sp, bottom: 20.sp, top: 20.sp);
  }
}
